package com.software.observer;

import java.util.ArrayList;
import java.util.List;

//观察者模式是要用被观察者类里使用观察者
public class Thief {

    private String name;
    private int age;

    private List<Police> policeList = new ArrayList<>();

    //添加观察者列表
    public void addObserverList() {
        policeList.add(new CameraPolice());
        policeList.add(new DriverPolice());
        policeList.add(new OperativePolice());
    }

    public Thief(String name, int age) {
        this.name = name;
        this.age = age;
        addObserverList();
    }

    //2，被观察者出现了指定的事件， 才能激活观察者。
    public void stolenMobile() {
        System.out.println("开始偷手机");
        notifyPolice();
    }

//    3，观察者如何处理被观察者的事件

    public void notifyPolice() {
        for (Police police : policeList) {
            police.CatchThief(this);
        }
    }

    @Override
    public String toString() {
        return "Thief{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
